const Model = require('../dbs/model/getCollection.js')
const NoteSchema = require('../dbs/schema/noteSchema.js')
const result = require("../com/result.js")

//复杂查询
// Note
// .where('mobile').equals('18875027064'),
// .where('age').gte(25)
// .where('tags').in(['movie', 'music', 'art'])
// .select('name', 'age', 'tags')
// .skip(20)
// .limit(10)
// .asc('age')
// .slaveOk()
// .hint({ age: 1, name: 1 })
// .exec((error,doc)=>{});

/**
 * 塞入短信记录
 * @param {*} ctx 
 * @param {*} next 
 */
let noteSave = async (ctx, next) => {
    let param = ctx.request.body;
    if (!param.mobile || !param.type) {
        ctx.body = result.go(result.code.CODE_501);
        return;
    }

    let NoteModel = await Model.getComModel({
        dataBase: 'com_info',
        tableName: 'note',
        schema: NoteSchema
    });

    let data={
        mobile: param.mobile,
        type: param.type
    };

    if(param.code){
        data.code=param.code;
    }

    // 创建实例
    let noteDocment = new NoteModel(data);
    //插入数据
    noteDocment.save();

    ctx.body = result.go(result.code.CODE_200);

    return;
};

/**
 * 查询短信记录
 * @param {*} ctx 
 * @param {*} next 
 */
let noteFind = async (ctx, next) => {
    let param = ctx.request.body;
    let query = {};
    if (param.mobile) {
        query.mobile = param.mobile;
    }
    if (param.type) {
        query.type = param.type;
    }
    let NoteModel = await Model.getComModel({
        dataBase: 'com_info',
        tableName: 'note',
        schema: NoteSchema
    });
    return await new Promise((res, rej) => {
        NoteModel.find(query, async (err, doc) => {
            ctx.body = result.go(result.code.CODE_200, doc);
            return res();
        });
    });
}

module.exports = {
    noteSave,
    noteFind
}  